package com.goal98.team.services

import com.goal98.team.domain.Person
import com.goal98.team.domain.Team
import com.goal98.grails.plugin.account.domain.Account

class MyteamService {

    def authenticateService

    boolean transactional = true

    def getMyteam() {

        def user = authenticateService.userDomain()

        Person person = Person.get(user.id)

        return person.team

    }

    def inMyTeam(id){

        if(id){
            Person person = Person.get(id)
            if(person?.team?.id){
                return person?.team?.id == getMyteam()?.id
            }else{
                return false
            }
        }else{
            log.error("Person ID parameter is null.")
            return false
        }

    }

    def playersInMyTeam(){
        return getMyteam()?.members
    }

    def openAccount(Team team) {
        if(team.account != null)
            throw new Exception("Team ${team} already has a account!")
        Account account = new Account(reference:team.name, balance:0)
        team.account = account
        account.save()
        team.save()

    }
}
